package com.example.finalapplication;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class XuqinActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{
   //ProgressBar bar;
   ListView listView;
   Handler handler;
     SimpleAdapter listItemAdapter;
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuqin);
        Intent intent4 = getIntent();
        //bar = findViewById(R.id.progressBar4);
      // listView.setEmptyView(bar);
        listView = findViewById(R.id.mylist4);
         ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        listItemAdapter = new SimpleAdapter(this,
                listItems,
                R.layout.item_one,
                new String[]{"ItemTitle1", "ItemDetail1"},
                new int[]{R.id.title1, R.id.detail1});

        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        handler = new Handler(Looper.myLooper()) {

            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    list = (ArrayList<HashMap<String, String>>) msg.obj;
                    listItemAdapter = new SimpleAdapter(XuqinActivity.this,
                            list,
                            R.layout.item_one,
                            new String[]{"cname", "cval"},
                            new int[]{R.id.title1, R.id.detail1}
                    );
             MineAdapterOne mineAdapterOne=new MineAdapterOne(XuqinActivity.this,
                            R.layout.item_one,list);

                    listView.setAdapter(listItemAdapter);
                }
                super.handleMessage(msg);
            }
        };

        FirstTask FirstTask = new FirstTask();
        FirstTask.setHandler(handler);

        Thread t = new Thread(FirstTask);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG, "onItemClick:position=" + position);
        // Log.i(TAG,"onItemClick:cname=" +map.get("cname"));
        //  Log.i(TAG,"onItemClick:cval=" +map.get("cval"));
        String titleStr = map.get("cname");
        String detailStr = map.get("cval");

        Log.i(TAG, "onItemClick:titleStr=" + titleStr);
        Log.i(TAG, "onItemClick:detailStr=" + detailStr);

        TextView title = (TextView) view.findViewById(R.id.title1);
        TextView detail = (TextView) view.findViewById(R.id.detail1);

        String title2 = String.valueOf(title.getText());
        String detail2 = String.valueOf(detail.getText());
        Log.i(TAG, "onItemClick:title2=" + title2);
        Log.i(TAG, "onItemClick:detail2=" + detail2);

       //跳转新界面
        Intent Calculation1 = new Intent(XuqinActivity.this, Calculation1.class);
        Calculation1.putExtra("title", titleStr);
        Calculation1.putExtra("rate", detailStr);

        startActivity(Calculation1);
    }
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: pos=" + position);//删除数据
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick:对话框事件处理");
                        list.remove(listView.getItemAtPosition(position));
                        listItemAdapter.notifyDataSetChanged();
                    }
                }).setNegativeButton("否", null);

        Log.i(TAG, "onItemLongClick:size=" + list.size());
        builder.create().show();
        return true;
    }

}
